package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.TimeAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.DingDanBean.Days;
import com.klhappy.houyuan.bean.DingDanBean.Hour;
import com.klhappy.houyuan.bean.DingDanBean.TimeBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2015/11/17.
 */
public class DeliverTimeActivity extends AbsActivity implements CallBackInterModle {

    @InjectView(R.id.today)
    Button today;
    @InjectView(R.id.tomorrow)
    Button tomorrow;
    @InjectView(R.id.after_tomorrow)
    Button after_tomorrow;
    @InjectView(R.id.date)
    Button date;

    @InjectView(R.id.gv_time)
    GridView gv_time;
    @InjectView(R.id.title)
    TextView title;


    @OnClick(R.id.today)
    public void today() {
        setView();
        today.setTextColor(whirt);
        today.setBackgroundResource(R.drawable.time_chose);
        day = days.get(0).getDay();
    }

    @OnClick(R.id.tomorrow)
    public void tomorrow() {
        setView();
        tomorrow.setTextColor(whirt);
        tomorrow.setBackgroundResource(R.drawable.time_chose);
        day = days.get(1).getDay();
    }

    @OnClick(R.id.after_tomorrow)
    public void after_tomorrow() {
        setView();
        after_tomorrow.setTextColor(whirt);
        after_tomorrow.setBackgroundResource(R.drawable.time_chose);
        day = days.get(2).getDay();
    }

    @OnClick(R.id.date)
    public void date() {
        setView();
        date.setTextColor(whirt);
        date.setBackgroundResource(R.drawable.time_chose);
        day = days.get(3).getDay();
    }

    @OnClick(R.id.click_ra)
    public void click_ra() {
        intent.putExtra("day", day);
        intent.putExtra("time", time);
        intent.putExtra("data", data);
        setResult(10, intent);
        finish();
    }

    private int whirt = 0xFFFFFFFF;
    private int green = 0xFF639800;
    private HouYuanModle modle;
    private TimeBean timeBean;
    private List<Days> days;
    private List<Hour> hours;

    private String day;
    private String time;
    private String data;
    private Intent intent;
    private TimeAdapter adapter;
    private int clickPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deliver_time);
        ButterKnife.inject(this);
        title.setText("送货时间");

        intent = new Intent();
        modle = new HouYuanModle(this);
        modle.setCallBackInterModle(this);
        modle.getTime();
        showDialog("请稍等...");
    }

    private void setView() {
        today.setTextColor(green);
        tomorrow.setTextColor(green);
        after_tomorrow.setTextColor(green);
        date.setTextColor(green);
        today.setBackgroundResource(R.drawable.time_no);
        tomorrow.setBackgroundResource(R.drawable.time_no);
        after_tomorrow.setBackgroundResource(R.drawable.time_no);
        date.setBackgroundResource(R.drawable.time_no);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mConfigManage.set(KeyConstant.DAY, day);
        mConfigManage.set(KeyConstant.TIME, time);
        mConfigManage.set(KeyConstant.DATA, data);
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.TIME_URL)) {
            if (o.getCode() == 200) {
                Gson gson = new Gson();
                Log.e("this", gson.toJson(o));
                timeBean = JsonCode.getClass(o.getEntity(), TimeBean.class);
                days = JsonCode.getClassList(timeBean.getDays(), Days.class);
                hours = JsonCode.getClassList(timeBean.getHour(), Hour.class);
                try {
                    setButtn();
                }catch (Exception e){

                }
            }
            closeDialog();
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            intent.putExtra("day", day);
            intent.putExtra("time", time);
            intent.putExtra("data", data);
            setResult(10, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setButtn() {
        adapter = new TimeAdapter(this ,hours);
        gv_time.setAdapter(adapter);

        gv_time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != clickPosition) {
                    clickPosition = position;
                } else {
                    clickPosition = -1;
                }
                time = hours.get(position).getAm();
                data = hours.get(position).getPoint();
                adapter.setNum(clickPosition);
            }
        });


        today.setText(days.get(0).getDesc());
        tomorrow.setText(days.get(1).getDesc());
        after_tomorrow.setText(days.get(2).getDesc());
        date.setText(days.get(3).getDesc());

        day = days.get(0).getDay();
        time = hours.get(0).getAm();
        data = hours.get(0).getPoint();
    }
}
